package peng.controller.AppResult;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import peng.entity.AdminUserGroup;
import peng.entity.AdminUserGroupMenuKey;
import peng.entity.AdminUserMenu;
import peng.service.AdminUserGroupMenuService;
import peng.service.AdminUserGroupService;
import peng.service.AdminUserMenuService;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/UserGroup")
public class AdminUserGroupController {
    /**
     * userGroupService 角色service
     * userGroupMenuService 角色权限service
     * userMenuService 权限service
     */
    @Resource
    private AdminUserGroupService userGroupService;
    @Resource
    private AdminUserGroupMenuService userGroupMenuService;
    @Resource
    private AdminUserMenuService userMenuService;

    /**
     * 删除角色
     */
    @RequestMapping("/deleteGroup")
    @ResponseBody
    public ResultMessage deleteByPrimaryKey(Integer id) {
        /*
        查询角色下是否有权限   若有删除权限
        showlist 查看角色下是否有权限
        deleteGroupMen  删除权限
        deleteByPrimaryKey 删除角色
         */
        if (userGroupMenuService.showlist(id).size()>0) {
            System.out.println("-------------------值"+userGroupMenuService.showlist(id));
            if (userGroupMenuService.deleteGroupMen(id) > 0) {
                if (userGroupService.deleteByPrimaryKey(id) > 0) {
                    return new ResultMessage(ResultMessage.code_OK_1, "删除角色成功");
                }
            } else {
                return new ResultMessage(ResultMessage.code_OK_1, "删除权限失败");
            }
        } else {
            if (userGroupService.deleteByPrimaryKey(id) > 0) {
                return new ResultMessage(ResultMessage.code_OK_1, "删除角色成功");
            }
            return new ResultMessage(ResultMessage.code_FAIL_0, "删除角色失败");
        }
        return new ResultMessage(ResultMessage.code_FAIL_0, "删除角色失败");
    }

    /**
     * 添加角色
     */
    @RequestMapping("/addGroup")
    @ResponseBody
    public ResultMessage insertSelective(AdminUserGroup record) {

        if (userGroupService.insertSelective(record) > 0) {
            return new ResultMessage(ResultMessage.code_OK_1, "成功");
        }
        return new ResultMessage(ResultMessage.code_FAIL_0, "失败");
    }

    /**
     * 查询角色
     */
    @RequestMapping("/showGroup")
    @ResponseBody
    public ResultMessage selectByPrimaryKey(Integer id) {

        if (userGroupService.selectByPrimaryKey(id) != null) {

            return new ResultMessage(ResultMessage.code_OK_1, "成功");
        }
        return new ResultMessage(ResultMessage.code_FAIL_0, "失败");
    }

    /**
     * 修改角色权限
     */
    @RequestMapping("/updateGroup")
    @ResponseBody
    public ResultMessage updateByPrimaryKeySelective(AdminUserGroup record) {
        if (userGroupService.updateByPrimaryKeySelective(record) > 0) {
            return new ResultMessage(ResultMessage.code_OK_1, "成功");
        }
        return new ResultMessage(ResultMessage.code_FAIL_0, "失败");
    }

    /**
     * 添加角色权限
     */

    @RequestMapping("/addGroupMen")
    @ResponseBody
    public String insertSelective(Integer id, Integer gid) {
        if (id != null) {
            AdminUserGroupMenuKey record = new AdminUserGroupMenuKey();
            record.setGid(gid);
            record.setMid(id);
            System.out.println("------------id" + id + "-------------------gid" + gid + "/n--------------" + userGroupMenuService.insertSelective(record));
            if (userGroupMenuService.insertSelective(record) > 0) {
                return "ok";
            }
            return "error";
        } else {
            return "error";
        }


    }

    /**
     * 查询角色权限
     */
    @RequestMapping("/show")
    public String show(Integer gid, Model model, HttpSession session) {
        session.setAttribute("gid", gid);
        List<AdminUserMenu> listmens = userMenuService.listmens();
        List<AdminUserGroupMenuKey> mids = userGroupMenuService.showlist(gid);

        if (listmens != null) {
            for (int i=0;i<listmens.size();i++){
                for (int j=0;j<mids.size();j++){
                    if (listmens.get(i).getId()==mids.get(j).getMid()){
                        listmens.remove(i);//去除重复的数据
                    }
                }

            }
            model.addAttribute("list1", listmens);
        }
        if (mids != null) {
            model.addAttribute("list2", mids);
            return "Men";
        } else {
            model.addAttribute("error", "没有权限，请添加！");
        }
        return "error";

    }

    /**
     * 修改角色权限
     */
    @RequestMapping("/updateGM")
    @ResponseBody
    public ResultMessage updateGM(AdminUserGroup adminUserGroup, Integer newMid) {
        AdminUserGroupMenuKey record = new AdminUserGroupMenuKey();
        int b = 0;
        for (AdminUserGroupMenuKey i : adminUserGroup.getKeys()) {
            record.setGid(i.getGid());
            record.setMid(i.getMid());
            record.setUmid(i.getUmid());
            b = userGroupMenuService.updateSelective(record);
        }
        //System.out.println("______________________________________"+adminUserGroupMenuKey.getGid()+"__________" +adminUserGroupMenuKey.getMid() );
        if (b > 0) {
            return new ResultMessage(ResultMessage.code_OK_1, "成功");
        }
        return new ResultMessage(ResultMessage.code_FAIL_0, "失败");
    }

    /**
     * 删除角色权限
     */
    @RequestMapping("/deleteGm")
    @ResponseBody
    public String deleteGm(Integer mid, Integer gid) {
        if (mid != null) {
            System.out.println("------------id" + mid + "-------------------gid" + gid + "--------------结果" );
            if (userGroupMenuService.deleteByPrimaryKey(gid,mid) > 0) {
                return "ok";
            }
            return "error";
        } else {
            return "error";
        }

    }

}
